<?php
class bill extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览订单', '1-2-3');
		$da = $this -> pub -> get_setting('订单', 'Bill');
		$this -> load -> view('adminx/bill', $da);
	}

	function get() {
		$this -> pub -> logp('获取订单数据', '1-2-3');
		$day1 = strtotime($this -> input -> post('day1'));
		$day2 = strtotime($this -> input -> post('day2')) + 86400;

		$this -> db -> where('mktime >', $day1);
		$this -> db -> where('mktime <', $day2);

		//商品id
		if (is_numeric($this -> input -> post('key_sp'))) {
			$this -> db -> where('bill_id in (select bill_id from pan_bill_item where sp_id=' . $this -> input -> post('key_sp') . ')');
		}
		//订单id
		if (is_numeric($this -> input -> post('key_bill_id'))) {
			$this -> db -> where('bill_id', $this -> input -> post('key_bill_id'));
		}
		if (is_numeric($this -> input -> post('key_phone'))) {
			$this -> db -> where('phone', $this -> input -> post('key_phone'));
		}
		//检索app过来的订单
		if ($this -> config -> item('is_app') && $this -> input -> post('zflx')) {
			if ($this -> input -> post('zflx') == 1) {
				$this -> db -> where('zflx', 1);
			} else {
				$this -> db -> where('zflx', 0);
			}
		}

		//设置权限
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
		}
		if ($this -> input -> post('key')) {
			$this -> db -> select('count(*)');
			if (is_numeric($this -> input -> post('key'))) {
				$this -> db -> where('pan_bill.bill_id', $this -> input -> post('key'));
			} else {
				$this -> db -> like('pan_bill.name', $this -> input -> post('key'));
			}

			if ($this -> input -> post('state') > 0) {
				$this -> db -> where('state', $this -> input -> post('state'));
			}
			if ($this -> input -> post('state') == -1) {
				$this -> db -> where('state', 0);
			}
			$q = $this -> db -> get_where('bill');

			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			if (is_numeric($this -> input -> post('key'))) {
				$this -> db -> where('pan_bill.bill_id', $this -> input -> post('key'));
			} else {
				$this -> db -> like('pan_bill.name', $this -> input -> post('key'));
			}
		} else {
			if ($this -> input -> post('state') > 0) {
				$this -> db -> where('state', $this -> input -> post('state'));
			}
			if ($this -> input -> post('state') == -1) {
				$this -> db -> where('state', 0);
			}
			$this -> db -> select('count(*)');

			$q = $this -> db -> get_where('bill');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('bill_id', 'desc');
		if ($this -> input -> post('state') > 0) {
			$this -> db -> where('pan_bill.state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('state') == -1) {
			$this -> db -> where('pan_bill.state', 0);
		}

		//设置权限
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
		}

		$this -> db -> where('mktime >', $day1);
		$this -> db -> where('mktime <', $day2);

		if (is_numeric($this -> input -> post('key_sp'))) {
			$this -> db -> where('bill_id in (select bill_id from pan_bill_item where sp_id=' . $this -> input -> post('key_sp') . ')');
		}

		//检索app过来的订单
		if ($this -> config -> item('is_app') && $this -> input -> post('zflx')) {
			if ($this -> input -> post('zflx') == 1) {
				$this -> db -> where('zflx', 1);
			} else {
				$this -> db -> where('zflx', 0);
			}
		}

		if (is_numeric($this -> input -> post('key_bill_id'))) {
			$this -> db -> where('bill_id', $this -> input -> post('key_bill_id'));
		}

		if (is_numeric($this -> input -> post('key_phone'))) {
			$this -> db -> where('phone', $this -> input -> post('key_phone'));
		}

		$q = $this -> db -> get_where('bill');
		$da['content'] = '';
		$state[0] = '未支付';
		$state[1] = '未发货';
		$state[2] = '已发货';
		$state[3] = '已收到';
		$state[4] = '作废订单';
		foreach ($q->result_array() as $v) {

			$this -> db -> select('nickname,headimgurl');
			$q = $this -> db -> get_where('member', array('mid' => $v['mid']));
			$m_ls = $q -> row_array(0);

			$da['content'] .= '<tr>';

			$da['content'] .= '<td><a target="_blank" href="/iadmin/member/info/' . $v['mid'] . '"><img src="' . ($m_ls['headimgurl'] ? $m_ls['headimgurl'] : '/mex/lance/img/tx.jpg') . '" width="60" style="float:left;" /></a></td>';
			$da['content'] .= '<td> ' . $v['name'] . '(' . $m_ls['nickname'] . ')' . '<br /> ' . $v['phone'];
			if ($v['ly']) {
				$da['content'] .= '<br> 留言： ' . $v['ly'];
			}

			$da['content'] .= '</td> ';

			$jp = $v['is_jp'] == 2 ? '竞拍，' : '';
			if ($v['thfs'] == 1) {
				$da['content'] .= '<td>' . $jp . $v['dq'] . ' ' . $v['zz'] . '</td>';
			} elseif ($v['is_jp'] == 3) {
				$this -> db -> select('bm_info');
				$this -> db -> join('sp', 'pan_sp.sp_id=pan_bill_item.sp_id');
				$q = $this -> db -> get_where('bill_item', array('bill_id' => $v['bill_id']));
				$sp = $q -> row_array(0);
				$da['content'] .= '<td>报名信息：' . $sp['bm_info'] . '</td>';
			} else {
				$this -> db -> select('jg_name,jg_phone');
				$q = $this -> db -> get_where('staff', array('staff_id' => $v['thshop']));
				$thshop = $q -> row_array(0);
				$da['content'] .= '<td>' . $jp . '到店提货：' . $thshop['jg_name'] . ' ' . $thshop['jg_phone'] . '</td>';
			}

			$da['content'] .= '<td class="text-danger" style="font-size:16px;font-weight: bolder;">' . $state[$v['state']] . '</td>';
			$da['content'] .= '<td>' . $v['numb'] . '</td>';
			$da['content'] .= '<td>' . $v['je'] . '</td>';
			$da['content'] .= '<td>' . date('Y-m-d H:i', $v['mktime']) . '</td>';

			$btn = '';
			if ($v['wl_hao']) {
				$btn .= ' <a target="_blank" href="/iadmin/bill/wl/' . $v['bill_id'] . '"  class="btn"><i class="fa fa-truck"></i> 物流</a> ';
			}
			if ($v['state'] == 1) {
				$btn .= ' <button onclick="show_bill_up(\'' . $v['bill_id'] . '\')" type="button" class="btn blue"><i class="fa fa-send"></i> 发货</button> ';
			}
			if ($v['state'] == 2 && $this -> session -> userdata('state') != 3) {
				$btn .= ' <button onclick="doup_billx(\'' . $v['bill_id'] . '\',3)" type="button" class="btn green"><i class="fa fa-edit"></i> 确定收货</button> ';
			}
			
			$btn .= ' <button onclick="doup_dz(\'' . $v['bill_id'] . '\')" type="button" class="btn green"><i class="fa fa-edit"></i> 修改</button> ';
			
			if ($v['is_wxzf'] == 1) {
				$btn .= ' <button onclick="get_zfxx(\'' . $v['bill_id'] . '\')" type="button" class="btn purple"><i class="fa fa-eye"></i> 支付跟踪</button>';
			}
			if ($v['state'] == 1 && $v['thfs'] == 2) {
				$btn .= ' <button onclick="get_ewm(\'' . $v['bill_id'] . '\',\'' . $v['name'] . '的提货\',5)" type="button" class="btn dark"><i class="fa fa-qrcode"></i> 二维码</button> ';
			}
			if ($this -> session -> userdata('state') != 3) {
				$btn .= ' <button onclick="doup_billx(\'' . $v['bill_id'] . '\',4)" type="button" class="btn red"><i class="fa fa-trash-o"></i> 作废订单</button> ';
			}
			if ($v['state'] == 0 || $v['state'] == 4) {
				$btn .= ' <button onclick="get_back_bill(\'' . $v['bill_id'] . '\',\'' . ($v['zflx'] == 1 ? 'app' : 'js') . '\')" type="button" class="btn red"><i class="icon-refresh"></i> 重新同步支付</button> ';
			}

			$da['content'] .= '<td>' . $btn . '</td>';

			$da['content'] .= '</tr>';

			$da['content'] .= '<tr>';
			$da['content'] .= '<td colspan="9"><table class="table table-hover table-responsive table-bordered" style="">';
			$this -> db -> join('sp', 'pan_sp.sp_id=pan_bill_item.sp_id');
			$q = $this -> db -> get_where('bill_item', array('bill_id' => $v['bill_id']));
			$sp = $q -> result_array();
			foreach ($sp as $vv) {
				$da['content'] .= '<tr>';
				$da['content'] .= '<td width="48"><img width="48" src="' . ($vv['pic1'] ? $this -> pub -> oss_dz($vv['pic1']) : '/style/images/logo.png') . '" /></td>';
				$da['content'] .= '<td>' . $vv['sp_name'] . ' ' . $vv['sp_color'] . '</td>';
				$da['content'] .= '<td title="价格" style="text-align: center;vertical-align: middle;width: 100px;">' . $v['bill_id'] . '<br>订单编号</td>';
				if ($v['color']) {
					$da['content'] .= '<td title="颜色" style="text-align: center;vertical-align: middle;width: 100px;">' . $v['color'] . '<br>颜色或样式</td>';
				}
				$da['content'] .= '<td title="价格" style="text-align: center;vertical-align: middle;width: 100px;">' . $vv['sp_price'] . '<br>价格</td>';
				if ($vv['sk_is'] == 1) {
					$da['content'] .= '<td title="供货款" style="text-align: center;vertical-align: middle;width: 100px;color:red;">' . $vv['sk_je'] . '<br>供货款</td>';
				}
				$da['content'] .= '<td  title="代理" style="text-align: center;vertical-align: middle;width: 100px;">' . $vv['sp_dl'] . '<br>一级代理</td>';
				$da['content'] .= '<td  title="代理" style="text-align: center;vertical-align: middle;width: 100px;">' . $vv['sp_dl1'] . '<br>二级代理</td>';
				$da['content'] .= '<td  title="代理" style="text-align: center;vertical-align: middle;width: 100px;">' . $vv['sp_dl2'] . '<br>三级代理</td>';
				$da['content'] .= '<td  title="礼券" style="text-align: center;vertical-align: middle;width: 100px;">' . $vv['sp_lq'] . '<br>礼券</td>';
				$da['content'] .= '<td  title="数量" style="text-align: center;vertical-align: middle;width: 100px;">' . $vv['numb'] . '<br>数量</td>';
				$da['content'] .= '<td  title="金额" style="text-align: center;vertical-align: middle;width: 100px;">' . $vv['je'] . '<br>金额</td>';
				$da['content'] .= '</tr>';
			}
			$da['content'] .= '</table></td></tr>';
		}
		$da['type'] = 1;

		//统计
		if (is_numeric($this -> input -> post('key'))) {
			$this -> db -> where('pan_bill.bill_id', $this -> input -> post('key'));
		} elseif ($this -> input -> post('key')) {
			$this -> db -> like('pan_bill.name', $this -> input -> post('key'));
		}

		//设置权限
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
		}

		$this -> db -> where('mktime >', $day1);
		$this -> db -> where('mktime <', $day2);

		if (is_numeric($this -> input -> post('key_sp'))) {
			$this -> db -> where('bill_id in (select bill_id from pan_bill_item where sp_id=' . $this -> input -> post('key_sp') . ')');
		}

		if ($this -> input -> post('state') > 0) {
			$this -> db -> where('state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('state') == -1) {
			$this -> db -> where('state', 0);
		}
		$this -> db -> select('sum(je),sum(numb)');

		if (is_numeric($this -> input -> post('key_bill_id'))) {
			$this -> db -> where('bill_id', $this -> input -> post('key_bill_id'));
		}

		if (is_numeric($this -> input -> post('key_phone'))) {
			$this -> db -> where('phone', $this -> input -> post('key_phone'));
		}

		$q = $this -> db -> get_where('bill');
		$ls = $q -> row_array(0);
		$da['tj_je'] = $ls['sum(je)'] ? $ls['sum(je)'] : '0';
		$da['tj_numb'] = $ls['sum(numb)'] ? $ls['sum(numb)'] : '0';

		echo json_encode($da);
	}

	function bill_up() {
		ignore_user_abort(true);
		//设置权限
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
		}

		$this -> db -> select('bill_id,mid,tj_mid,je,state');
		$q = $this -> db -> get_where('bill', array('bill_id' => $this -> input -> post('idx')));
		$bill = $q -> row_array(0);
		if ($bill['state'] != $this -> input -> post('state') || $this -> input -> post('state') == 2) {
			if ($this -> input -> post('wl_name')) {
				$da['wl_name'] = $this -> input -> post('wl_name');
			}
			if ($this -> input -> post('wl_hao')) {
				$da['wl_hao'] = $this -> input -> post('wl_hao');
			}
			$da['state'] = $this -> input -> post('state');
			//设置权限
			if ($this -> session -> userdata('state') == 3) {
				$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
			}
			$this -> db -> update('bill', $da, array('bill_id' => $this -> input -> post('idx')));

			$this -> db -> where('mid', $bill['mid']);
			$q = $this -> db -> get_where('member');
			$m = $q -> row_array(0);

			$this -> db -> select('sp_name,sp_dl,sp_dl1,sp_dl2,sp_lq,numb,sp_id');
			$q = $this -> db -> get_where('bill_item', array('bill_id' => $this -> input -> post('idx')));
			$sp = $q -> result_array();
			$sp_name = '';
			$lq = 0;
			$dl = 0;
			$dl1 = 0;
			$dl2 = 0;
			foreach ($sp as $v) {
				$sp_name .= $v['sp_name'] . ' ';
				$lq += $v['sp_lq'];
				$dl += $v['sp_dl'] * $v['numb'];

				$dl1 += $v['sp_dl1'] * $v['numb'];
				$dl2 += $v['sp_dl2'] * $v['numb'];
			}

			if ($da['state'] == 2) {
				$this -> pub -> logp('发货 - ' . $this -> input -> post('idx'), '1-2-3');
				$this -> pub -> send_zd_text($m['openid'], '【' . sprintf("%08d", $this -> input -> post('idx')) . '】您购买的<' . $sp_name . '>已发货！' . PHP_EOL . PHP_EOL . '<a href=\'' . $this -> config -> item('base_urlx') . '/shop/bill\'>查询订单</a>');
			}

			//通知用户已经发货
			if ($da['state'] == 3) {
				$this -> pub -> logp('确认收货 - ' . $this -> input -> post('idx'), '1-2');
				//发送红包
				$this -> pub -> send_jl($bill['mid'], $this -> input -> post('idx'));
			}

			if ($da['state'] == 4 && $bill['state'] > 0) {
				$this -> pub -> logp('作废订单 - ' . $this -> input -> post('idx'), '1-2-3');
				//返还现金 -2是购物  -4 是竞拍 -3活动
				//$this -> db -> update('jb', array('state' => -1), array('did' => '-4-' . $this -> input -> post('idx')));
				//$this -> db -> update('jb', array('state' => -1), array('did' => '-2-' . $this -> input -> post('idx')));
				//$this -> db -> update('jb', array('state' => -1), array('did' => '-3-' . $this -> input -> post('idx')));

				//$this -> pub -> send_zd_text($m['openid'], '您购买的<' . $sp_name . '>订单作废，款项已经退回！' . PHP_EOL . PHP_EOL . '<a href=\'' . $this -> config -> item('base_urlx') . '/shop/bill\'>查询订单</a>');
			}
			echo json_encode(array('type' => 1));
		} else {
			$this -> pub -> logp('订单操作失败 - ' . $this -> input -> post('idx'), '1-2-3');
			echo json_encode(array('type' => 2));
		}

	}

	function get_bill_one() {
		$this -> pub -> logp('获取订单信息 - ' . $this -> input -> post('idx'), '1-2-3');
		//设置权限
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
		}

		$q = $this -> db -> get_where('bill', array('bill_id' => $this -> input -> post('idx')));
		$da = $q -> row_array(0);
		$da['content'] .= '<p>客户：' . $da['name'] . '<p/>';
		$da['content'] .= '<p>电话：' . $da['phone'] . '<p/>';
		$da['content'] .= '<p>金额：<span id="bill_up_je">' . $da['je'] . '</span> <p/>';
		$da['content'] .= '<div id="show_bill_up_je" style="display:none;"><div class="input-group" >
      <input type="text" class="form-control" value="' . $da['je'] . '">
      <span class="input-group-btn">
        <button class="btn btn-default" type="button" onclick="do_show_bill_up_je($(this),\'' . $this -> input -> post('idx') . '\')">设置金额</button>
      </span>
    </div><br /></div>';
		$da['content'] .= '<p>地址：' . $da['dq'] . ' ' . $da['zz'] . '<p/>';
		$da['typex'] = 1;
		echo json_encode($da);
	}

	//获取支付信息
	function get_zfxx() {
		$this -> pub -> logp('获取支付信息 - ' . $this -> input -> post('bill_id'), '1-2-3');
		$da = '';
		$q = $this -> db -> get_where('zf', array('out_trade_no' => $this -> input -> post('bill_id')));
		if ($q -> num_rows() > 0) {
			$ls = $q -> row_array(0);
			$da .= '<p>交易模式：' . $ls['trade_mode'] . '</p>';
			$da .= '<p>交易状态：' . $ls['trade_state'] . '</p>';
			$da .= '<p>商户号：' . $ls['partner'] . '</p>';
			$da .= '<p>付款银行：' . $ls['bank_type'] . '</p>';
			$da .= '<p>银行订单号：' . $ls['bank_billno'] . '</p>';
			$da .= '<p>总金额：' . $ls['total_fee'] . '</p>';
			$da .= '<p>币种：' . $ls['fee_type'] . '</p>';
			$da .= '<p>通知 ID：' . $ls['notify_id'] . '</p>';
			$da .= '<p>订单号：' . $ls['transaction_id'] . '</p>';
			$da .= '<p>商户订单号：' . $ls['out_trade_no'] . '</p>';
			$da .= '<p>商户数据包：' . $ls['attach'] . '</p>';
			$da .= '<p>支付完成时间：' . $ls['time_end'] . '</p>';
			$da .= '<p>物流费用：' . $ls['transport_fee'] . '</p>';
			$da .= '<p>物品费用：' . $ls['product_fee'] . '</p>';
			$da .= '<p>折扣价格：' . $ls['discount'] . '</p>';
		} else {
			$da = '<p>对方没有支付！</p>';
		}
		echo $da;
	}

	function wl($id) {
		$this -> pub -> logp('查看物流信息 - ' . $id, '1-2-3');
		$da = $this -> pub -> get_setting('订单物流', 'Bill');

		$q = $this -> db -> get_where('bill', array('bill_id' => $id));
		$da['bill'] = $q -> row_array(0);

		$q = $this -> db -> get_where('member', array('mid' => $da['bill']['mid']));
		$da['m'] = $q -> row_array(0);

		$this -> db -> join('sp', 'pan_sp.sp_id=pan_bill_item.sp_id');
		$q = $this -> db -> get_where('bill_item', array('bill_id' => $da['bill']['bill_id']));
		$da['bill_item'] = $q -> result_array();

		//红包发放情况
		$this -> db -> select('pan_jb.*,pan_member.headimgurl,pan_member.nickname,pan_member.js,pan_member.yh,pan_member.openid');
		$this -> db -> join('member', 'pan_member.mid=pan_jb.mid');
		$q = $this -> db -> get_where('jb', array('did' => '1-' . $id));
		$da['jb1'] = $q -> result_array();
		$this -> db -> select('pan_jb.*,pan_member.headimgurl,pan_member.nickname,pan_member.js,pan_member.yh,pan_member.openid');
		$this -> db -> join('member', 'pan_member.mid=pan_jb.mid');
		$q = $this -> db -> get_where('jb', array('did' => '-2-' . $id));
		$da['jb2'] = $q -> result_array();

		$this -> db -> select('pan_jb.*,pan_member.headimgurl,pan_member.nickname,pan_member.js,pan_member.yh,pan_member.openid');
		$this -> db -> join('member', 'pan_member.mid=pan_jb.mid');
		$q = $this -> db -> get_where('jb', array('did' => '4-' . $id));
		$da['jb3'] = $q -> result_array();
		$this -> db -> select('pan_jb.*,pan_member.headimgurl,pan_member.nickname,pan_member.js,pan_member.yh,pan_member.openid');
		$this -> db -> join('member', 'pan_member.mid=pan_jb.mid');
		$q = $this -> db -> get_where('jb', array('did' => '5-' . $id));
		$da['jb4'] = $q -> result_array();
		$this -> db -> select('pan_jb.*,pan_member.headimgurl,pan_member.nickname,pan_member.js,pan_member.yh,pan_member.openid');
		$this -> db -> join('member', 'pan_member.mid=pan_jb.mid');
		$q = $this -> db -> get_where('jb', array('did' => '6-' . $id));
		$da['jb5'] = $q -> result_array();

		$type = $this -> config -> item('wl');
		$a = file_get_contents('http://www.kuaidi100.com/query?type=' . $type[$da['bill']['wl_name']] . '&postid=' . $da['bill']['wl_hao'] . '&id=1');

		$da['wl'] = json_decode($a, true);

		$da['type'] = $this -> config -> item('jb_type');

		//获取上级分销商户

		$da['ms'] = $this -> pub -> get_san($da['m']['mid']);

		$this -> load -> view('adminx/wl', $da);
	}

	//修改物流
	function wl_up() {
		//设置权限
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
		}

		if ($this -> input -> post('wl_name')) {
			$da['wl_name'] = $this -> input -> post('wl_name');
		}
		if ($this -> input -> post('wl_hao')) {
			$da['wl_hao'] = $this -> input -> post('wl_hao');
		}

		$this -> db -> update('bill', $da, array('bill_id' => $this -> input -> post('idx')));
		echo json_encode(array('type' => 1));
	}

}
?>